package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Page;
import com.dongdongshop.data.PageBean;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.*;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.concurrent.CountDownLatch;

@Controller
@RequestMapping("goodsController")
public class GoodsController {

    @Reference(timeout = 5000)
    private ItemCatService itemCatService;
    @Reference
    private TempLateService tempLateService;
    @Reference
    private GoodsService goodsService;
    @Reference
    private GoodsDescService goodsDescService;
    @Reference
    private SpecificationService specificationService;

    @RequestMapping("toGoods")
    public String toGoods(Model model){
        List<TbItemCat> itemList = itemCatService.selectByItemCatByParentId(0L);
        model.addAttribute("itemList",itemList);
        return "admin/goods_edit";
    }

    @RequestMapping("selectByItemCatByParentId/{pid}")
    @ResponseBody
    public Result selectByItemCatByParentId(@PathVariable("pid") Long pid){
        List<TbItemCat> itemCatList = itemCatService.selectByItemCatByParentId(pid);
        return Result.build(ConstantEnum.SUCCESS).setResult(itemCatList);
    }

    @RequestMapping("getItemTypeId/{pid}")
    @ResponseBody
    public Result getItemTypeId(@PathVariable("pid") Long pid){
        List<TbItemCat> itemCatList = itemCatService.getItemTypeId(pid);
        return Result.build(ConstantEnum.SUCCESS).setResult(itemCatList);
    }

    @RequestMapping("getBrandId")
    @ResponseBody
    public Result getBrandId(Long id){
        List<TbTypeTemplate> list = tempLateService.getBrandId(id);
        return Result.build(ConstantEnum.SUCCESS).setResult(list);
    }

//    @RequestMapping("addInfo")
//    @ResponseBody
//    public Result addInfo(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc){
//        Subject subject = SecurityUtils.getSubject();
//        TbSeller tbSeller =(TbSeller) subject.getPrincipal();
//        boolean b =  goodsService.inserGoods(tbSeller.getSellerId(),tbGoods,tbGoodsDesc);
//        return Result.build(ConstantEnum.SUCCESS);
//    }

    @RequestMapping("selectOption")
    @ResponseBody
    public Result selectOption(Long id){
        try {
            List<OptionVO> optionVO = specificationService.selectData(id);
            return Result.build(ConstantEnum.SUCCESS).setResult(optionVO);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR);
        }
    }

    @RequestMapping("insertGoods")
    @ResponseBody
    public Result insertGoods(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc,TbItem tbItem){
        Subject subject = SecurityUtils.getSubject();
        TbSeller tbSeller =(TbSeller) subject.getPrincipal();
        tbGoods.setSellerId(tbSeller.getSellerId());
        tbItem.setSellerId(tbSeller.getSellerId());
        tbItem.setSeller(tbSeller.getNickName());

        goodsService.insertGoods(tbGoods,tbGoodsDesc,tbItem);
        return Result.build(ConstantEnum.SUCCESS);
    }

    @RequestMapping("toGoodss")
    public String toGoodss(){
        return "admin/goods";
    }

    @RequestMapping("listGoods")
    @ResponseBody
    public Result listGoods(String auditStatus,String goodsName,@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "5")Integer pageSize){
            String id = "";
            PageBean<GoodsVO> list = goodsService.listGoods(id, auditStatus, goodsName,pageNum,pageSize);
            return Result.build(ConstantEnum.SUCCESS).setResult(list);

    }
}
